//
//  MainViewController.m
//  cherryPicker
//
//  Created by Young Soo Yun on 12. 1. 2..
//  Copyright (c) 2012 Happy. All rights reserved.
//

#import "MainViewController.h"

#import "MainTableViewCell.h"
#import "InsertModalViewController.h"
#import "SettingViewController.h"
#import "Options.h"
#import "CherryPickerDB.h"
#import "CardDetailViewController.h"
#import "ADControl.h"
#import "DateUtil.h"

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [[ADControl defaultsAD] addAds:self wihtYPoint:0];
    
    if ([[Options getValue:OptionUser1 valueName:@"modal"] isEqualToString:@"1"]) {
        InsertModalViewController *insertVC = [[InsertModalViewController alloc] initWithNibName:@"InsertModalViewController" bundle:nil];
        [self presentModalViewController:insertVC animated:YES];
        [insertVC release];
    }
    
    NSString* thisMonth = [[DateUtil formatYYMMDDFromDateStr:[DateUtil nowDateStr]] substringWithRange:NSMakeRange(5, 2)];
    NSString* nextMonth = [[DateUtil formatYYMMDDFromDateStr:[DateUtil nextDateStr:1]] substringWithRange:NSMakeRange(5, 2)];

    
    _labThisMonth.text = [NSString stringWithFormat:@"%d월 결제금", [thisMonth intValue]];
    _labNextMonth.text = [NSString stringWithFormat:@"%d월 결제금", [nextMonth intValue]];
    
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)viewWillAppear:(BOOL)animated{
    [_tvCardList reloadData];
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	
	return 49;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray* arrCardList = [CherryPickerDB selectFromCardList];
    CardListDB* cardDB = [arrCardList objectAtIndex:indexPath.row];
    
    CardDetailViewController *detailVC = [[CardDetailViewController alloc] initWithNibName:@"CardDetailViewController" bundle:nil];
    [[self navigationController] pushViewController:detailVC animated:YES];
    [detailVC initViewData:cardDB];
    [detailVC release];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section {
    NSArray* arrCardList = [CherryPickerDB selectFromCardList];
    
    return arrCardList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	int row = [indexPath row];
    
    MainTableViewCell * cell = (MainTableViewCell *)[tv dequeueReusableCellWithIdentifier:@"MainTableViewCell"];
    if(cell == nil) {
        NSArray * array = [[NSBundle mainBundle] loadNibNamed:@"MainTableViewCell" owner:self options:nil];
        cell = (MainTableViewCell*)[array objectAtIndex:0];
    }
    NSArray* arrCardList = [CherryPickerDB selectFromCardList];
    CardListDB* cardDB = [arrCardList objectAtIndex:row];
    [cell setData:cardDB._CardName strDelimiter:cardDB._Delimiter
    strTotalPrice:[CherryPickerDB selectFromReceiptForSum:cardDB._Delimiter]
     limitPrice:cardDB._LimitPrice];
    
    return cell;
}

#pragma - IBAction

- (IBAction)openInsertModalVC{
    InsertModalViewController *insertVC = [[InsertModalViewController alloc] initWithNibName:@"InsertModalViewController" bundle:nil];
    [self presentModalViewController:insertVC animated:YES];
    [insertVC release];
}

- (IBAction)goSettingVC{
    SettingViewController *settingVC = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
    [[self navigationController] pushViewController:settingVC animated:YES];
    [settingVC release];
}

@end
